package info.thereisonlywe.core.essentials;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.util.Calendar;
import java.util.logging.Level;

public class SystemEssentials {

	private static final boolean android = System.getProperties()
			.get("java.runtime.name").equals("android runtime");

	public static class Logger {

		private static String tobewritten = ""; // we will write these later on.

		private static String getTime()
		{
			Calendar cal = Calendar.getInstance();
			String time = TimeEssentials.formatTime(
					cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),
					cal.get(Calendar.SECOND))
					+ " on "
					+ (cal.get(Calendar.DAY_OF_MONTH) + 1)
					+ "/"
					+ cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.YEAR);
			return time;
		}

		private static void write(String st)
		{
			try
			{
				FileWriter fstream = new FileWriter("log.txt", true);
				BufferedWriter out = new BufferedWriter(fstream);
				String stx = tobewritten + st;
				out.write(stx);
				tobewritten = "";
				out.flush();
				out.close();
			}
			catch (Exception e)
			{
				tobewritten += st + "\n";
			}
		}

		public static synchronized void flush()
		{
			String st = tobewritten;
			tobewritten = "";
			write(st + "\n");
		}

		public static synchronized void log(Level a, Exception b)
		{
			if (a.equals(Level.SEVERE) || a.equals(Level.WARNING)) writeF(b
					.getMessage());
			else writeI(b.getMessage());
		}

		public static synchronized void log(Level a, String b)
		{
			if (a.equals(Level.SEVERE) || a.equals(Level.WARNING)) writeF(b);
			else writeI(b);
		}

		// for writing information logs to file
		private static void writeI(String info)
		{
			String st = "[I] ";
			st += info + " @ " + getTime();
			System.out.println(st);
			write(st + "\n");
		}

		// for writing failure logs to file
		private static void writeF(String info)
		{
			String st = "[E] ";
			st += info
					+ " IN "
					+ new Throwable().fillInStackTrace().getStackTrace()[2]
							.getMethodName()
					+ " {"
					+ new Throwable().fillInStackTrace().getStackTrace()[2]
							.getLineNumber()
					+ "}, "
					+ new Throwable().fillInStackTrace().getStackTrace()[2]
							.getClassName() + " @ " + getTime();
			System.err.println(st);
			write(st + "\n");
		}
	}

	public static String runCommand(String cmd)
	{
		try
		{
			StringBuilder result = new StringBuilder("");
			String s;
			Process p = Runtime.getRuntime().exec(cmd);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			while ((s = in.readLine()) != null)
			{
				result.append(s);
			}
			return result.toString();
		}
		catch (IOException ex)
		{
			Logger.log(Level.WARNING, ex);
		}
		return null;
	}

	public static void collectGarbage()
	{
		Object obj = new Object();
		WeakReference ref = new WeakReference(obj);
		obj = null;
		while (ref.get() != null)
		{
			System.gc();
		}
	}

	public static void sleep(long echoDelay)
	{
		try
		{
			Thread.sleep(echoDelay);
		}
		catch (InterruptedException ex)
		{
		}
	}

	public static boolean isRunningOnAndroid()
	{
		return android;
	}

}
